package com.ruoyi.order.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.order.domain.OrderInfo;
import com.ruoyi.order.service.IOrderInfoService;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 订单Controller
 * 
 * @author DEREK
 * @date 2023-05-22
 */
@RestController
@RequestMapping("/order/orderInfo")
public class OrderInfoController extends BaseController
{
    @Autowired
    private IOrderInfoService orderInfoService;

    /**
     * 查询订单列表
     */
    @PreAuthorize("@ss.hasPermi('order:orderInfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(OrderInfo orderInfo)
    {
    	Long userId = getUserId();
        startPage();
        if(1 == orderInfo.getManageType()) {
        	orderInfo.setManageId(userId);
		 }
        if(StringUtils.isNotEmpty(orderInfo.getBegTime()) && StringUtils.isNotEmpty(orderInfo.getEndTime())) {
    		orderInfo.setBegTime(orderInfo.getBegTime().replace("-", ""));
    		orderInfo.setEndTime(orderInfo.getEndTime().replace("-", ""));
    	}
        List<OrderInfo> list = orderInfoService.selectOrderInfoList(orderInfo);
        if(userId.longValue() > 3) {
        	return getDataTable(list,"0");
        }else {
        	return getDataTable(list,orderInfoService.statisticsOrderAmount(orderInfo));
        	
        }
        
        
    }
    
    @PreAuthorize("@ss.hasPermi('order:orderInfo:join')")
	 @Log(title = "将订单加入当前登录账户旗下", businessType = BusinessType.UPDATE)
	 @GetMapping("/joinManage")
	 public AjaxResult joinManage(Long id)
	 {
    	OrderInfo orderInfo = orderInfoService.getOne(new LambdaQueryWrapper<OrderInfo>().eq(OrderInfo::getId, id)
    			.select(OrderInfo::getId,OrderInfo::getManageId,OrderInfo::getManageName));
		 if(orderInfo.getManageId() > 0) {
			 return AjaxResult.error("当前用户已经加入 "+orderInfo.getManageName()+" 旗下");
		 }
		 orderInfo.setManageId(getUserId());
		 orderInfo.setManageName(getUsername());
		 orderInfoService.updateById(orderInfo);
		 return AjaxResult.success("加入成功");
		  
	 }
	 
	 @PreAuthorize("@ss.hasPermi('order:orderInfo:remove')")
	 @Log(title = "将订单移除当前账号", businessType = BusinessType.UPDATE)
	 @GetMapping("/handleDelOrder")
	 public AjaxResult handleDelOrder(Long id)
	 {
		 OrderInfo orderInfo = orderInfoService.getOne(new LambdaQueryWrapper<OrderInfo>().eq(OrderInfo::getId, id)
				 .select(OrderInfo::getId,OrderInfo::getManageId,OrderInfo::getManageName));
		 orderInfo.setManageId(0L);
		 orderInfo.setManageName("");
		 orderInfoService.updateById(orderInfo);
		 return AjaxResult.success("移除成功");
	 }
	 

    /**
     * 导出订单列表
     */
    @PreAuthorize("@ss.hasPermi('order:orderInfo:export')")
    @Log(title = "订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OrderInfo orderInfo)
    {
        List<OrderInfo> list = orderInfoService.selectOrderInfoList(orderInfo);
        ExcelUtil<OrderInfo> util = new ExcelUtil<OrderInfo>(OrderInfo.class);
        util.exportExcel(response, list, "订单数据");
    }

    /**
     * 获取订单详细信息
     */
    @PreAuthorize("@ss.hasPermi('order:orderInfo:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(orderInfoService.selectOrderInfoById(id));
    }

    /**
     * 新增订单
     */
    @PreAuthorize("@ss.hasPermi('order:orderInfo:add')")
    @Log(title = "订单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OrderInfo orderInfo)
    {
        return toAjax(orderInfoService.insertOrderInfo(orderInfo));
    }

    /**
     * 修改订单
     */
    @PreAuthorize("@ss.hasPermi('order:orderInfo:edit')")
    @Log(title = "订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OrderInfo orderInfo)
    {
        return toAjax(orderInfoService.updateOrderInfo(orderInfo));
    }

    /**
     * 删除订单
     */
    @PreAuthorize("@ss.hasPermi('order:orderInfo:remove1')")
    @Log(title = "订单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
    	Long id = ids[0];
    	if(null != id) {
    		OrderInfo info = new OrderInfo();
    		info.setId(id);
    		info.setOrderStatus("10");
    		orderInfoService.updateById(info);
    	}
    	
    	return toAjax(1);
//        return toAjax(orderInfoService.deleteOrderInfoByIds(ids));
    }
}
